﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Workbench.Presentation.UserContols
{
    public partial class ClickToEditTextboxControl : UserControl
    {
        public ClickToEditTextboxControl()
        {
            InitializeComponent();
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =DependencyProperty.Register("Text", typeof(string), typeof(ClickToEditTextboxControl), new UIPropertyMetadata());

        private void textBoxName_LostFocus(object sender, RoutedEventArgs e)
        {
            ShowTextBlock();
        }

        private void textBlockName_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ShowTextBox();
        }

        void ShowTextBlock()
        {
            textBoxName.Visibility = Visibility.Collapsed;
            textBlockName.Visibility = Visibility.Visible;
        }

        void ShowTextBox()
        {
            textBoxName.Visibility = Visibility.Visible;
            textBlockName.Visibility = Visibility.Collapsed;
        }

        private void grid_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                if (textBoxName.Visibility == Visibility.Visible) ShowTextBlock();
                else ShowTextBox();
            }
        }
    }
}
